package com.data.modules.online.cgform.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.data.common.system.api.ISysBaseAPI;
import com.data.common.system.vo.DictModel;
import com.data.common.system.vo.LoginUser;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.auth.service.IOnlAuthPageService;
import com.data.modules.online.cgform.entity.OnlCgformButton;
import com.data.modules.online.cgform.entity.OnlCgformEnhanceJs;
import com.data.modules.online.cgform.entity.OnlCgformField;
import com.data.modules.online.cgform.entity.OnlCgformHead;
import com.data.modules.online.cgform.model.*;
import com.data.modules.online.cgform.constant.LinkDown;
import com.data.modules.online.cgform.extend.ExtendJsonKey;
import com.data.modules.online.cgform.service.IOnlCgformFieldService;
import com.data.modules.online.cgform.service.IOnlCgformHeadService;
import com.data.modules.online.cgform.service.IOnlineService;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.util.EnhanceJsUtil;
import com.data.modules.online.cgform.util.OnlFormShowType;
import com.data.modules.online.cgform.util.OnlSlotRender;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Slf4j
@SuppressWarnings("ALL")
@Service
public class OnlineServiceImpl implements IOnlineService {
    @Autowired
    private IOnlAuthPageService onlAuthPageService;
    @Autowired
    private IOnlCgformFieldService onlCgformFieldService;
    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    private static /* synthetic */ java.lang.Object m199a(java.lang.invoke.SerializedLambda r4) {
        /*
        // Method dump skipped, instructions count: 364
        */
        throw new UnsupportedOperationException("Method not decompiled: com.data.modules.online.cgform.service.p013a.OnlineServiceImpl.m199a(java.lang.invoke.SerializedLambda):java.lang.Object");
    }

    @Override
    public OnlComplexModel queryOnlineConfig(OnlCgformHead head, String username) {
        String id = head.getId();
        boolean a = CgformUtil.m258a(head);
        List<OnlCgformField> b = m205b(id);
        List<String> queryHideCode = this.onlAuthPageService.queryHideCode(id, true);
        ArrayList arrayList = new ArrayList();
        HashMap hashMap = new HashMap();
        ArrayList arrayList2 = new ArrayList();
        ArrayList arrayList3 = new ArrayList();
        ArrayList arrayList4 = new ArrayList();
        HashMap hashMap2 = new HashMap();
        for (OnlCgformField onlCgformField : b) {
            String dbFieldName = onlCgformField.getDbFieldName();
            String mainTable = onlCgformField.getMainTable();
            String mainField = onlCgformField.getMainField();
            if (ConvertUtils.isNotEmpty(mainField) && ConvertUtils.isNotEmpty(mainTable)) {
                arrayList3.add(new OnlForeignKey(dbFieldName, mainField));
            }
            if (1 == onlCgformField.getIsShowList().intValue() && !"id".equals(dbFieldName) && !queryHideCode.contains(dbFieldName) && !arrayList4.contains(dbFieldName)) {
                OnlColumn a2 = m201a(onlCgformField, hashMap, arrayList2);
                String linkField = a2.getLinkField();
                if (linkField != null && !"".equals(linkField)) {
                    m202a(b, arrayList4, arrayList, dbFieldName, linkField);
                }
                hashMap2.put(onlCgformField.getDbFieldName(), 1);
                arrayList.add(a2);
            }
        }
        if (a) {
            List<OnlColumn> a3 = m200a(head, hashMap, arrayList2, hashMap2);
            if (a3.size() > 0) {
                ArrayList arrayList5 = new ArrayList();
                for (Object str : hashMap2.keySet()) {
                    if (Integer.parseInt((String) hashMap2.get(str)) > 1) {
                        arrayList5.add(str);
                    }
                }
                for (OnlColumn onlColumn : a3) {
                    String dataIndex = onlColumn.getDataIndex();
                    if (arrayList5.contains(dataIndex)) {
                        onlColumn.setDataIndex(onlColumn.getTableName() + "_" + dataIndex);
                    }
                    arrayList.add(onlColumn);
                }
            }
        }
        OnlComplexModel onlComplexModel = new OnlComplexModel();
        onlComplexModel.setCode(id);
        onlComplexModel.setTableType(head.getTableType());
        onlComplexModel.setFormTemplate(head.getFormTemplate());
        onlComplexModel.setDescription(head.getTableTxt());
        onlComplexModel.setCurrentTableName(head.getTableName());
        onlComplexModel.setPaginationFlag(head.getIsPage());
        onlComplexModel.setCheckboxFlag(head.getIsCheckbox());
        onlComplexModel.setScrollFlag(head.getScroll());
        onlComplexModel.setRelationType(head.getRelationType());
        onlComplexModel.setColumns(arrayList);
        onlComplexModel.setDictOptions(hashMap);
        onlComplexModel.setFieldHrefSlots(arrayList2);
        onlComplexModel.setForeignKeys(arrayList3);
        onlComplexModel.setHideColumns(queryHideCode);
        List<OnlCgformButton> queryButtonList = this.onlCgformHeadService.queryButtonList(id, true);
        ArrayList arrayList6 = new ArrayList();
        for (OnlCgformButton onlCgformButton : queryButtonList) {
            if (!queryHideCode.contains(onlCgformButton.getButtonCode())) {
                arrayList6.add(onlCgformButton);
            }
        }
        onlComplexModel.setCgButtonList(arrayList6);
        OnlCgformEnhanceJs queryEnhanceJs = this.onlCgformHeadService.queryEnhanceJs(id, CgformUtil.f277am);
        if (queryEnhanceJs != null && ConvertUtils.isNotEmpty(queryEnhanceJs.getCgJs())) {
            onlComplexModel.setEnhanceJs(EnhanceJsUtil.getDeleteActionOrButton(queryEnhanceJs.getCgJs(), queryButtonList));
        }
        if ("Y".equals(head.getIsTree())) {
            onlComplexModel.setPidField(head.getTreeParentIdField());
            onlComplexModel.setHasChildrenField(head.getTreeIdField());
            onlComplexModel.setTextField(head.getTreeFieldname());
        }
        return onlComplexModel;
    }

    private String[] m204a(String str) {
        String[] strArr = {"", ""};
        if (str != null && !"".equals(str)) {
            JSONObject parseObject = JSON.parseObject(str);
            if (parseObject.containsKey(ExtendJsonKey.f90d)) {
                strArr[0] = ConvertUtils.camelToUnderline(parseObject.getString(ExtendJsonKey.f90d));
            }
            if (parseObject.containsKey(ExtendJsonKey.f91e)) {
                strArr[1] = ConvertUtils.camelToUnderline(parseObject.getString(ExtendJsonKey.f91e));
            }
        }
        return strArr;
    }

    private void m202a(List<OnlCgformField> list, List<String> list2, List<OnlColumn> list3, String str, String str2) {
        if (ConvertUtils.isNotEmpty(str2)) {
            String[] split = str2.split(CgformUtil.f231E);
            for (String str3 : split) {
                Iterator<OnlCgformField> it = list.iterator();
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    OnlCgformField next = it.next();
                    String dbFieldName = next.getDbFieldName();
                    if (1 == next.getIsShowList().intValue() && str3.equals(dbFieldName)) {
                        list2.add(str3);
                        OnlColumn onlColumn = new OnlColumn(next.getDbFieldTxt(), dbFieldName);
                        onlColumn.setCustomRender(str);
                        list3.add(onlColumn);
                        break;
                    }
                }
            }
        }
    }

    @Override
    public JSONObject queryOnlineFormObj(OnlCgformHead head, OnlCgformEnhanceJs onlCgformEnhanceJs) {
        JSONObject jSONObject = new JSONObject();
        String id = head.getId();
        String taskId = head.getTaskId();
        List<OnlCgformField> queryAvailableFields = this.onlCgformFieldService.queryAvailableFields(id, head.getTableName(), taskId, false);
        ArrayList arrayList = new ArrayList();
        if (ConvertUtils.isEmpty(taskId)) {
            List<String> queryFormDisabledCode = this.onlAuthPageService.queryFormDisabledCode(head.getId());
            if (!(queryFormDisabledCode == null || queryFormDisabledCode.size() <= 0 || queryFormDisabledCode.get(0) == null)) {
                arrayList.addAll(queryFormDisabledCode);
            }
        } else {
            List<String> queryDisabledFields = this.onlCgformFieldService.queryDisabledFields(head.getTableName(), taskId);
            if (!(queryDisabledFields == null || queryDisabledFields.size() <= 0 || queryDisabledFields.get(0) == null)) {
                arrayList.addAll(queryDisabledFields);
            }
        }
        EnhanceJsUtil.setOnlCgformEnhanceJsValue(onlCgformEnhanceJs, head.getTableName(), queryAvailableFields);
        TreeSelectColumn fVar = null;
        if ("Y".equals(head.getIsTree())) {
            fVar = new TreeSelectColumn();
            fVar.setCodeField("id");
            fVar.setFieldName(head.getTreeParentIdField());
            fVar.setPidField(head.getTreeParentIdField());
            fVar.setPidValue("0");
            fVar.setHsaChildField(head.getTreeIdField());
            fVar.setTableName(CgformUtil.m285f(head.getTableName()));
            fVar.setTextField(head.getTreeFieldname());
        }
        JSONObject a = CgformUtil.m222a(queryAvailableFields, arrayList, fVar);
        a.put("table", head.getTableName());
        a.put("describe", head.getTableTxt());
        jSONObject.put("schema", a);
        jSONObject.put("head", head);
        List<OnlCgformButton> queryFormValidButton = queryFormValidButton(id);
        if (queryFormValidButton != null && queryFormValidButton.size() > 0) {
            jSONObject.put("cgButtonList", queryFormValidButton);
        }
        if (onlCgformEnhanceJs != null && ConvertUtils.isNotEmpty(onlCgformEnhanceJs.getCgJs())) {
            onlCgformEnhanceJs.setCgJs(EnhanceJsUtil.getButton(onlCgformEnhanceJs.getCgJs(), queryFormValidButton));
            jSONObject.put("enhanceJs", EnhanceJsUtil.getDeleteAction(onlCgformEnhanceJs.getCgJs()));
        }
        return jSONObject;
    }

    @Override
    @Cacheable(key = "'erp'+ #head.id+'-'+#username", value = {"sys:cache:online:form"})
    public JSONObject queryOnlineFormObj(OnlCgformHead head, String username) {
        return queryOnlineFormObj(head, this.onlCgformHeadService.queryEnhanceJs(head.getId(), CgformUtil.f276al));
    }

    @Override
    public List<OnlCgformButton> queryFormValidButton(String headId) {
        List<OnlCgformButton> var2 = this.onlCgformHeadService.queryButtonList(headId, false);
        List var3 = null;
        if (var2 != null && var2.size() > 0) {
            LoginUser var4 = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String var5 = var4.getId();
            List var6 = this.onlAuthPageService.queryFormHideButton(var5, headId);
            var3 = (List)var2.stream().filter((var1) -> {
                return var6 == null || var6.indexOf(var1.getButtonCode()) < 0;
            }).collect(Collectors.toList());
        }

        return var3;
    }

    private static /* synthetic */ boolean m203a(List list, OnlCgformButton onlCgformButton) {
        return list == null || list.indexOf(onlCgformButton.getButtonCode()) < 0;
    }

    @Override
    @Cacheable(key = "#head.id+'-'+#username", value = {"sys:cache:online:form"})
    public JSONObject queryOnlineFormItem(OnlCgformHead head, String username) {
        head.setTaskId(null);
        return m198a(head);
    }

    @Override
    @Cacheable(key = "'flow' + #head.id + '-' + #username + '-' + #taskId", value = {"sys:cache:online:form"})
    public JSONObject queryFlowOnlineFormItem(OnlCgformHead head, String username, String taskId) {
        head.setTaskId(taskId);
        return m198a(head);
    }

    @Override
    @Cacheable(key = "'enhancejs' + #code + '-' + #type", value = {"sys:cache:online:form"})
    public String queryEnahcneJsString(String code, String type) {
        OnlCgformEnhanceJs queryEnhanceJs = this.onlCgformHeadService.queryEnhanceJs(code, type);
        if (queryEnhanceJs == null || !ConvertUtils.isNotEmpty(queryEnhanceJs.getCgJs())) {
            return "";
        }
        return EnhanceJsUtil.getDeleteActionOrButton(queryEnhanceJs.getCgJs(), (List<OnlCgformButton>) null);
    }

    private List<OnlCgformField> m205b(String var1) {
        LambdaQueryWrapper<OnlCgformField> var2 = new LambdaQueryWrapper<OnlCgformField>();
        var2.eq(OnlCgformField::getCgformHeadId, var1);
        var2.orderByAsc(OnlCgformField::getOrderNum);
        //var2.orderByAsc(OnlCgformField.getOrderNum());
        return this.onlCgformFieldService.list(var2);
    }

    private JSONObject m198a(OnlCgformHead head) {
        OnlCgformEnhanceJs var2 = this.onlCgformHeadService.queryEnhanceJs(head.getId(), "form");
        JSONObject var3 = this.queryOnlineFormObj(head, var2);
        var3.put("formTemplate", head.getFormTemplate());
        if (head.getTableType() == 2) {
            JSONObject var4 = var3.getJSONObject("schema");
            String var5 = head.getSubTableStr();
            if (ConvertUtils.isNotEmpty(var5)) {
                ArrayList var6 = new ArrayList();
                String[] var7 = var5.split(",");
                int var8 = var7.length;

                for(int var9 = 0; var9 < var8; ++var9) {
                    String var10 = var7[var9];
                    OnlCgformHead var11 = (OnlCgformHead)this.onlCgformHeadService.getOne((Wrapper)(new LambdaQueryWrapper()).eq("table_name", var10));
                    if (var11 != null) {
                        var6.add(var11);
                    }
                }

                if (var6.size() > 0) {
                    Collections.sort(var6, new Comparator<OnlCgformHead>() {
                        @Override
                        public int compare(OnlCgformHead var1, OnlCgformHead var2) {
                            Integer var3 = var1.getTabOrderNum();
                            if (var3 == null) {
                                var3 = 0;
                            }
                            Integer var4 = var2.getTabOrderNum();
                            if (var4 == null) {
                                var4 = 0;
                            }
                            return var3.compareTo(var4);
                        }
                    });
                    Iterator var13 = var6.iterator();

                    while(var13.hasNext()) {
                        OnlCgformHead var14 = (OnlCgformHead)var13.next();
                        List<OnlCgformField> var15 = this.onlCgformFieldService.queryAvailableFields(var14.getId(), var14.getTableName(), head.getTaskId(), false);
                        EnhanceJsUtil.setOnlCgformEnhanceJsValue(var2, var14.getTableName(), var15);
                        JSONObject var16 = new JSONObject();
                        new ArrayList();
                        List<String> var17;
                        if (ConvertUtils.isNotEmpty(head.getTaskId())) {
                            var17 = this.onlCgformFieldService.queryDisabledFields(var14.getTableName(), head.getTaskId());
                        } else {
                            var17 = this.onlAuthPageService.queryFormDisabledCode(var14.getId());
                        }

                        if (1 == var14.getRelationType()) {
                            //var16 = b.a(var15, var17, (f)null);
                            var16 = CgformUtil.m222a(var15,var17,null);
                        } else {
                            var16.put("columns", CgformUtil.m220a(var15, var17));

                            List var12 = this.onlAuthPageService.queryListHideButton((String)null, var14.getId());
                            var16.put("hideButtons", var12);
                        }

                        String var18 = this.onlCgformFieldService.queryForeignKey(var14.getId(), head.getTableName());
                        var16.put("foreignKey", var18);
                        var16.put("id", var14.getId());
                        var16.put("describe", var14.getTableTxt());
                        var16.put("key", var14.getTableName());
                        var16.put("view", "tab");
                        var16.put("order", var14.getTabOrderNum());
                        var16.put("relationType", var14.getRelationType());
                        var16.put("formTemplate", var14.getFormTemplate());
                        var4.getJSONObject("properties").put(var14.getTableName(), var16);
                    }
                }
            }

            if (var2 != null && ConvertUtils.isNotEmpty(var2.getCgJs())) {
                var3.put("enhanceJs", EnhanceJsUtil.getDeleteAction(var2.getCgJs()));
            }
        }

        return var3;
    }

    private OnlColumn m201a(OnlCgformField onlCgformField, Map<String, List<DictModel>> map, List<HrefSlots> list) {
        JSONObject parseObject;
        String dbFieldName = onlCgformField.getDbFieldName();
        OnlColumn onlColumn = new OnlColumn(onlCgformField.getDbFieldTxt(), dbFieldName);
        String dictField = onlCgformField.getDictField();
        String fieldShowType = onlCgformField.getFieldShowType();
        if (ConvertUtils.isNotEmpty(dictField) && !CgformUtil.f239M.equals(fieldShowType)) {
            List<DictModel> arrayList = new ArrayList<>();
            if (ConvertUtils.isNotEmpty(onlCgformField.getDictTable())) {
                arrayList = this.sysBaseAPI.queryTableDictItemsByCode(onlCgformField.getDictTable(), onlCgformField.getDictText(), dictField);
            } else if (ConvertUtils.isNotEmpty(onlCgformField.getDictField())) {
                arrayList = this.sysBaseAPI.queryDictItemsByCode(dictField);
            }
            map.put(dbFieldName, arrayList);
            onlColumn.setCustomRender(dbFieldName);
        }
        if (CgformUtil.f238L.equals(fieldShowType)) {
            map.put(dbFieldName, CgformUtil.m239a(onlCgformField));
            onlColumn.setCustomRender(dbFieldName);
        }
        if (CgformUtil.f245S.equals(fieldShowType)) {
            LinkDown aVar = (LinkDown) JSONObject.parseObject(onlCgformField.getDictTable(), LinkDown.class);
            map.put(dbFieldName, this.sysBaseAPI.queryTableDictItemsByCode(aVar.getTable(), aVar.getTxt(), aVar.getKey()));
            onlColumn.setCustomRender(dbFieldName);
            onlColumn.setLinkField(aVar.getLinkField());
        }
        if (CgformUtil.f243Q.equals(fieldShowType)) {
            String[] split = onlCgformField.getDictText().split(CgformUtil.f231E);
            map.put(dbFieldName, this.sysBaseAPI.queryTableDictItemsByCode(onlCgformField.getDictTable(), split[2], split[0]));
            onlColumn.setCustomRender(dbFieldName);
        }
        if (CgformUtil.f244R.equals(fieldShowType)) {
            String dictText = onlCgformField.getDictText();
            if (ConvertUtils.isEmpty(dictText)) {
                map.put(dbFieldName, this.sysBaseAPI.queryFilterTableDictInfo(CgformUtil.f252Z, CgformUtil.f265aa, "ID", CgformUtil.m282e(onlCgformField.getDictField())));
                onlColumn.setCustomRender(dbFieldName);
            } else {
                onlColumn.setCustomRender("_replace_text_" + dictText);
            }
        }
        if ("sel_depart".equals(fieldShowType)) {
            String[] a = m204a(onlCgformField.getFieldExtendJson());
            map.put(dbFieldName, this.sysBaseAPI.queryTableDictItemsByCode(CgformUtil.f249W, a[1].length() > 0 ? a[1] : CgformUtil.f250X, a[0].length() > 0 ? a[0] : "ID"));
            onlColumn.setCustomRender(dbFieldName);
        }
        if ("sel_user".equals(onlCgformField.getFieldShowType())) {
            String[] a2 = m204a(onlCgformField.getFieldExtendJson());
            map.put(dbFieldName, this.sysBaseAPI.queryTableDictItemsByCode(CgformUtil.f246T, a2[1].length() > 0 ? a2[1] : CgformUtil.f247U, a2[0].length() > 0 ? a2[0] : CgformUtil.f248V));
            onlColumn.setCustomRender(dbFieldName);
        }
        if (fieldShowType.indexOf("file") >= 0) {
            onlColumn.setScopedSlots(new ScopedSlots(OnlSlotRender.f322a));
        } else if (fieldShowType.indexOf("image") >= 0) {
            onlColumn.setScopedSlots(new ScopedSlots(OnlSlotRender.f323b));
        } else if (fieldShowType.indexOf(OnlFormShowType.f319c) >= 0) {
            onlColumn.setScopedSlots(new ScopedSlots(OnlSlotRender.f324c));
        } else if (fieldShowType.equals(OnlFormShowType.f320d)) {
            onlColumn.setScopedSlots(new ScopedSlots(OnlSlotRender.f325d));
        } else if (fieldShowType.equals(OnlFormShowType.f321e)) {
            onlColumn.setScopedSlots(new ScopedSlots(OnlSlotRender.f327f));
        }
        if (StringUtils.isNotBlank(onlCgformField.getFieldHref())) {
            String str = OnlSlotRender.f326e + dbFieldName;
            onlColumn.setHrefSlotName(str);
            list.add(new HrefSlots(str, onlCgformField.getFieldHref()));
        }
        if ("1".equals(onlCgformField.getSortFlag())) {
            onlColumn.setSorter(true);
        }
        String fieldExtendJson = onlCgformField.getFieldExtendJson();
        if (ConvertUtils.isNotEmpty(fieldExtendJson) && fieldExtendJson.indexOf(ExtendJsonKey.f88b) > 0 && (parseObject = JSON.parseObject(fieldExtendJson)) != null && parseObject.get(ExtendJsonKey.f88b) != null) {
            onlColumn.setShowLength(ConvertUtils.getInt(parseObject.get(ExtendJsonKey.f88b)).intValue());
        }
        return onlColumn;
    }

private java.util.List<com.data.modules.online.cgform.model.OnlColumn> m200a(com.data.modules.online.cgform.entity.OnlCgformHead r11, java.util.Map<java.lang.String, java.util.List<com.data.common.system.vo.DictModel>> r12, java.util.List<com.data.modules.online.cgform.model.HrefSlots> r13, java.util.Map<java.lang.String, java.lang.Integer> r14) {
        /*
        // Method dump skipped, instructions count: 185
        */
        throw new UnsupportedOperationException("Method not decompiled: com.data.modules.online.cgform.service.p013a.OnlineServiceImpl.m200a(com.data.modules.online.cgform.entity.OnlCgformHead, java.util.Map, java.util.List, java.util.Map):java.util.List");
    }
}
